package com.xbts.screen.service.impl;

import com.xbts.common.core.enums.WorkshopTypeEnum;
import com.xbts.common.core.web.domain.BaseHistogramResult;
import com.xbts.common.core.web.domain.BasePieChartResult;
import com.xbts.screen.domain.workshop.dto.CommonQueryDTO;
import com.xbts.screen.domain.workshop.vo.*;
import com.xbts.screen.mapper.LineDetectionMapper;
import com.xbts.screen.service.ILineDetectionService;
import com.xbts.screen.service.IWorkshopCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 线路检测
 */
@Service
public class LineDetectionServerImpl implements ILineDetectionService {

    @Autowired
    private LineDetectionMapper lineDetectionMapper;
    @Autowired
    private IWorkshopCommonService workshopCommonService;

    @Override
    public RealTimeAchievementRateVO realTimeAchievementRate() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.realTimeAchievementRate(dto);
    }

    @Override
    public BaseHistogramResult tenDaysAchievementRate() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.tenDaysAchievementRate(dto);
    }

    @Override
    public StatisticalRateGroupByTimeVO achievementRateGroupByTimeType() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.achievementRateGroupByTimeType(dto);
    }

    @Override
    public BaseHistogramResult waterUnitConsume() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.waterUnitConsume(dto);
    }

    @Override
    public BaseHistogramResult electricityUnitConsume() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.electricityUnitConsume(dto);
    }

    @Override
    public List<ProductionScheduleVO> productionSchedule() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.productionSchedule(dto);
    }

    @Override
    public BaseHistogramResult turnoverRate() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.turnoverRate(dto);
    }

    @Override
    public StatisticalRateGroupByTimeVO turnoverRateGroupByTimeType() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.turnoverRateGroupByTimeType(dto);
    }


    @Override
    public BaseHistogramResult passRateTrend() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.passRateTrend(dto);
    }

    @Override
    public ScrapRateVO PCS_scrapPassRate() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.scrapRate(dto);
    }

    @Override
    public List<BasePieChartResult> PCS_scrapStatistics() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.scrapStatistics(dto);
    }

    @Override
    public List<PassRateRecordVO> passRateRecord() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.passRateRecord(dto);
    }

    @Override
    public BaseHistogramResult scrapRateTrend() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        BaseHistogramResult result = workshopCommonService.passRateTrend(dto);
        List<String> target = new ArrayList<>();
        List<String> actual = result.getActual();
        for (int i = 0; i < result.getXAxis().size(); i++) {
            target.add("0.5");
            String s = actual.get(i);
            String format = String.format("%.2f", 100 - Double.parseDouble(s));
            actual.set(i, format);
        }
        result.setTarget(target);
        return result;
    }

    @Override
    public List<BasePieChartResult> PCS_badStatistics() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.badStatistics(dto);
    }

    @Override
    public PassRateVO PCS_passRate() {
        CommonQueryDTO dto = new CommonQueryDTO();
        dto.setDeptNames(Arrays.asList(WorkshopTypeEnum.LINE_DETECTION.getErpValue().split(",")));
        return workshopCommonService.passRate(dto);
    }
}